#coding=utf-8
'''
Created on 2017年10月27日

@author: hadoop
挖掘中国天气网：http://www.weather.com.cn/weather/101190401.shtml
'''
import requests;
import random;
from bs4 import BeautifulSoup;
import csv;
import sys;

reload(sys);
sys.setdefaultencoding('utf-8')
print (sys.getdefaultencoding());

html_doc = "";
header = {
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; W…) Gecko/20100101 Firefox/56.0',
'Accept':'text/html,application/xhtml+xm…plication/xml;q=0.9,*/*;q=0.8',
'Accept-Language':'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding':'gzip, deflate',
'Connection':'keep-alive'};
timeout = random.randint(60,180);

#rep = requests.get("http://www.weather.com.cn/weather/101190401.shtml",headers = header,timeout = timeout);
rep = requests.get("http://www.weather.com.cn/weather/101190401.shtml",params={'headers':header,'timeout' :timeout});
rep.encoding = 'utf-8';

html_doc = rep.text;
soup = BeautifulSoup(html_doc, 'html.parser');
body = soup.body;

div = body.find('div', {'id':'7d'});
ul = div.find('ul');
lis = ul.find_all('li');

rows = [];
for li in lis:
    row = [];
    date = li.find('h1').string;
    
    p = li.find('p',{'class':'tem'});
    
    temperature_highest = p.find('span');
    if temperature_highest is None:
        temperature_highest = None;
    else:
        temperature_highest = temperature_highest.string;
    temperature_lowest = p.find('i').string;
    temperature_lowest = temperature_lowest.replace('℃','');
    
    print (date,temperature_highest,temperature_lowest);
    
    row.append(date);
    row.append(temperature_highest);
    row.append(temperature_lowest);
    rows.append(row);

myfile = open("C:/Users/Administrator/Desktop/test.csv", "w+" ,1);
my_csv = csv.writer(myfile);
for row in rows:
    my_csv.writerow(row);
myfile.close();
print (rows);


